package servlet.api;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import pojo.MerchantApplication;
import service.MerchantApplicationManager;
import service.factory.ManagerFactory;
import servlet.LoadWaitAuditServlet;
import util.JsonUtil;
import dto.MerchantDto;
import dto.ResponseDto;

public class GetMerchantByAuditServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MerchantApplicationManager manager = null;
//    private Logger logger = LogManager.getLogger(LoadWaitAuditServlet.class);   

    public GetMerchantByAuditServlet() {
        super();
        manager = ManagerFactory.getApplicationManagerInstance();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		String flag = request.getParameter("flag");
		List<MerchantApplication> merchants = manager.getByStatus(MerchantApplication.WAIT_AUDIT);
		
		List<MerchantDto> merchantDtos = new ArrayList<>();
		String path = (String) getServletContext().getAttribute("imagepath");
		for (MerchantApplication application : merchants) {
			MerchantDto merchantDto = new MerchantDto(application.getName(),path+application.getAvatar(),application.getAddress());
			merchantDtos.add(merchantDto);
		}
		ResponseDto responseDto = new ResponseDto(1,"success",merchantDtos);
		PrintWriter writer = response.getWriter();
		writer.write(JsonUtil.toJsonString(responseDto));
		writer.close();
	}

}
